import requests
session = requests.session()
host = "http://192.168.100.134:8080"
global_header = {"Content-Type":"application/json"}
#登录
def login(loginname,password,code):
    login_url = "/v7/login"
    login_data = {"loginname": loginname, "password": password,"code":code}
    login_response = session.post(url=host + login_url, json=login_data, headers=global_header)
    login_response.encoding = "UTF-8"
    login_code = login_response.status_code
    login_content = login_response.json()
    print(login_code)
    return login_code
    global token
    token = login_content['token']
    # print(host + login_url)
    # print(login_code)
    # # print(login_content)
    # print(login_content['token'])

#信息头
# login()
# total_header = global_header.copy()
# pim_header = {"Authorization":f"Bearer {token}"}
# total_header.update(pim_header)
#print(total_header)

#新增员工信息
def add(pimpersonname,ygbh,zjlx,zjhm,lxdh,ygzt):
    pim_url ="/pimpeople"
    pim_data={"pimpersonname":pimpersonname,"ygbh":ygbh,"zjlx":zjlx,"zjhm":zjhm,"lxdh":lxdh,"ygzt":ygzt}
    login()
    total_header = global_header.copy()
    pim_header = {"Authorization": f"Bearer {token}"}
    total_header.update(pim_header)
    pim_response=session.post(url=host+pim_url,json=pim_data,headers=total_header)
    pim_response.encoding="UTF-8"
    pim_response_status_code = pim_response.status_code
    print(pim_response_status_code)
    return pim_response_status_code

def upload_image():
    upload_image_url = "/ibizutil/upload"
    upload_image_file ={'file':open('E:\\photo\\tt.gif','rb')}
    upload_image_header = {"Content-Type": "multipart/form-data"}
    upload_image_response = session.post(url=host + upload_image_url, files=upload_image_file)
    upload_image_response.encoding="UTF-8"
    response_image_status_code = upload_image_response.status_code
    upload_image_content = upload_image_response.json()
    print(upload_image_content)
    # print(host+upload_image_url)
    print(response_image_status_code)
    global fileid
    fileid = upload_image_content['fileid']
    print(fileid)
    return response_image_status_code, fileid


# upload_image()
def download():
    upload_image()
    download_url =f"/ibizutil/download/{fileid}"
    download_response = session.get(url=host + download_url)
    download_response_code = download_response.status_code
    print(host + download_url)
    print(download_response_code)
    return download_response_code


